package logic

import (
	"bole/bokerpc/bokerpc"
	"bole/bokerpc/internal/svc"
	"bole/models/cache"
	"bole/models/mysql"
	"bole/pkg"
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *bokerpc.RegisterRequest) (*bokerpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	var finduser mysql.Boke
	mysql.DB.Debug().Where("account =?", in.Account).Find(&finduser)

	if finduser.ID != 0 {
		return nil, fmt.Errorf("用户已存在,请前去登陆")
	}
	result, _ := cache.Rdb.Get(l.ctx, "captcha"+in.Account).Result()
	if result != in.CaptchaCode {
		return nil, fmt.Errorf("图形验证码,获取失败")
	}
	//var findmobile mysql.Boke
	//mysql.DB.Debug().Where("mobile =?", in.Mobile).Find(&findmobile)
	//result1, _ := cache.Rdb.Get(l.ctx, "sendSms"+in.Mobile).Result()
	//if result1 != in.CaptchaCode {
	//	return nil, fmt.Errorf("手机号验证码,获取失败")
	//}
	boke := mysql.Boke{
		Account:  in.Account,
		Password: pkg.Md5(in.Password),
		Mobile:   in.Mobile,
	}
	mysql.DB.Debug().Create(&boke)
	return &bokerpc.RegisterResponse{
		Userid: int64(finduser.ID),
	}, nil
}
